import scrapy

from doubanspider.items import MovieItem


class MovieSpider(scrapy.Spider):
    name = "movie"
    allowed_domains = ["douban.com"]
    start_urls = ["https://movie.douban.com/top250"]

    custom_settings = {
        "DOWNLOAD_DELAY": 1,
        "ITEM_PIPELINES": {
            "doubanspider.pipelines.MovieMysqlPipeline": None,
            "doubanspider.pipelines.MovieJsonPipeline": 400,
        }
    }

    def parse(self, response):
        for subject_url in response.css("#content .item .hd > a::attr('href')").getall():
            yield scrapy.Request(url=subject_url, callback=self.parse_subject)

        next_url_param = response.css(
            "#content .paginator > span.next > a::attr('href')").get()
        yield scrapy.Request(url=self.start_urls[0] + next_url_param)

    def parse_subject(self, response):
        selector = response.css("#info")
        item = MovieItem()
        item["name"] = response.css("#content>h1>span::text").get()
        item["director"] = selector.css(
            "span:nth-of-type(1)>span.attrs>a::text").get()
        item["playwright"] = selector.css(
            "span:nth-of-type(2)>span.attrs>a::text").getall()
        item["actor"] = selector.css(
            "span.actor:nth-of-type(3)>span.attrs>a::text").getall()
        item["rating_num"] = response.css(
            "#interest_sectl strong.ll.rating_num::text").get()

        yield item
